/* 
 * File:   Game.cpp
 * Author: jose
 * 
 * Created on 5 de Abril de 2012, 20:28
 */

#include "Game.h"
#include "Paddle.h"
#include "Timer.h"

Game::Game() {
}

Game::Game(const Game& orig) {
}

Game::~Game() {
}

bool Game::init() {
    //Initialize all SDL subsystems
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
        return false;
    }

    //Set up the screen
    mainScreen = SDL_SetVideoMode(GameDefinitions::SCREEN_WIDTH,
            GameDefinitions::SCREEN_HEIGHT,
            GameDefinitions::SCREEN_BPP,
            SDL_SWSURFACE);

    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);

    //If there was an error in setting up the screen
    if (mainScreen == NULL) {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption(" ===> Ping Pong <====", NULL);

    //If everything initialized fine
    return true;
}

void Game::createDrawables() {
    player1 = new Paddle("player1.png");
    player1->posX = 10;
    player1->posY = (GameDefinitions::SCREEN_HEIGHT - player1->paddleImage->clip_rect.h) / 2;

    player2 = new Paddle("player2.png");
    player2->posX = GameDefinitions::SCREEN_WIDTH - player2->paddleImage->clip_rect.w - 10;
    player2->posY = (GameDefinitions::SCREEN_HEIGHT - player2->paddleImage->clip_rect.h) / 2;

    gameBall = new Ball("ball.png");
    gameBall->posX = GameDefinitions::SCREEN_WIDTH / 2;
    gameBall->posY = GameDefinitions::SCREEN_HEIGHT / 2;
}

void Game::start() {
    SDL_Event event;
    bool quit = false;
    Uint8 *keystates;

    init();

    createDrawables();

    Timer delta;
     delta.start();
     
    while (!quit) {
        while (SDL_PollEvent(&event)) {
       //If the user has Xed out the window
            if (event.type == SDL_QUIT) {
                //Quit the program
                quit = true;
            }

            //If a key was pressed
            if (event.type == SDL_KEYDOWN) {
                //Adjust the velocity
                switch (event.key.keysym.sym) {
                        //case SDLK_UP: player1->moveUp(delta.get_ticks()); break;
                        //case SDLK_DOWN: player1->moveDown(delta.get_ticks()); break;
                }
            }                //If a key was released
            else if (event.type == SDL_KEYUP) {
                //Adjust the velocity
                switch (event.key.keysym.sym) {
                        //case SDLK_UP: player1->moveUp(delta.get_ticks()); break;
                        //case SDLK_DOWN: player1->moveDown(delta.get_ticks()); break;
                }
            }

        }

        Uint8 *keystates = SDL_GetKeyState(NULL);


        //If up is pressed
        if (keystates[ SDLK_UP ]) {
            player1->moveUp(delta.get_ticks());
        }

        //If down is pressed
        if (keystates[ SDLK_DOWN ]) {
            player1->moveDown(delta.get_ticks());
        }


        //If up is pressed
        if (keystates[ SDLK_w]) {
            player2->moveUp(delta.get_ticks());
        }

        //If down is pressed
        if (keystates[ SDLK_s ]) {
            player2->moveDown(delta.get_ticks());
        }
        
        SDL_FillRect(mainScreen, &mainScreen->clip_rect, SDL_MapRGB(mainScreen->format, 0xFF, 0xFF, 0xFF));

        performCollisions();

        applyGameLogic(delta.get_ticks());

        drawItems();

        keystates = SDL_GetKeyState(NULL);

        if (keystates[SDLK_ESCAPE]) {
            quit = true;
        }

        delta.start();
        
        if (SDL_Flip(mainScreen) == -1)
            return;

        SDL_Delay(1); //Delay / pause
    }

    //Free the surface
    SDL_FreeSurface(player1->paddleImage);
    SDL_FreeSurface(player2->paddleImage);
    SDL_FreeSurface(gameBall->ballImage);

    //Quit SDL
    SDL_Quit();

}

void Game::applyGameLogic(Uint32 deltaTime) {
    gameBall->move(deltaTime);
}

void Game::performCollisions() {

}

void Game::drawItems() {
    SdlUtils::applySurface(player1->posX, player1->posY, player1->paddleImage, mainScreen);
    SdlUtils::applySurface(player2->posX, player2->posY, player2->paddleImage, mainScreen);
    SdlUtils::applySurface(gameBall->posX, gameBall->posY, gameBall->ballImage, mainScreen);
}

